package com.rc.saas.tenant.service.msg;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.config.UpYunConfig;
import com.rc.saas.tenant.mapper.msg.TenantNoticeMapper;
import com.rc.saas.tenant.model.banner.TenantBanner;
import com.rc.saas.tenant.model.msg.TenantNotice;
import com.rc.saas.tenant.model.msg.TenantNoticeExample;
import com.rc.saas.tenant.search.msg.TenantNoticeSearch;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author frog
 * @date 2019/12/17
 */
@Service
public class TenantNoticeServiceImpl implements TenantNoticeService {

    @Autowired
    private TenantNoticeMapper tenantNoticeMapper;

    @Autowired
    private UpYunConfig upYunConfig;

    @Override
    public TenantNoticeMapper getMapper() {
        return tenantNoticeMapper;
    }

    @Override
    public PageInfo<TenantNotice> pageTenantNotice(TenantNoticeSearch baseSearch) {
        PageHelper.startPage(baseSearch.getOffset(), baseSearch.getLimit());
        TenantNoticeExample example = new TenantNoticeExample();
        TenantNoticeExample.Criteria criteria = example.createCriteria();
        criteria.andTenantCodeEqualTo(baseSearch.getTenantCode());
        if(!StringUtils.isEmpty(baseSearch.getTitle())){
            criteria.andTitleLike("%"+baseSearch.getTitle()+"%");
        }
        List<TenantNotice> tenantNotices = tenantNoticeMapper.selectByExample(example);
        for (TenantNotice tenantNotice : tenantNotices) {
            tenantNotice.setImgUrl(upYunConfig.getDownloadRoot()+ tenantNotice.getImgUrl());
        }
        return new PageInfo<TenantNotice>(tenantNotices);
    }

    /**
     * 导出数据查询
     * @param tenantNoticeSearch
     * @return
     */
    @Override
    public PageInfo<TenantNotice> listTenantNotice(TenantNoticeSearch tenantNoticeSearch) {
        PageHelper.startPage(tenantNoticeSearch.getOffset(), tenantNoticeSearch.getLimit());
        TenantNoticeExample example = new TenantNoticeExample();
        example.createCriteria().andTenantCodeEqualTo(tenantNoticeSearch.getTenantCode());
        example.createCriteria().andTitleLike(tenantNoticeSearch.getTitle());
        List<TenantNotice> tenantNotices = tenantNoticeMapper.selectByExample(example);
        for (TenantNotice tenantNotice : tenantNotices) {
            tenantNotice.setImgUrl(upYunConfig.getDownloadRoot()+ tenantNotice.getImgUrl());
        }
        return new PageInfo<TenantNotice>(tenantNotices);
    }
}
